<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小组名称" prop="groupName">
        <el-input v-model="queryParams.groupName" placeholder="请输入小组名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建者" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输入创建者" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable v-model="queryParams.createTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['group:group:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['group:group:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['group:group:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['group:group:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="groupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" width="50px" />
      <el-table-column label="分组名称" align="center" prop="groupName" />
      <!-- <el-table-column label="专家组类型" align="center" prop="postName" /> -->
      <el-table-column label="专利列表" align="center">
        <template slot-scope="scope">
          <!-- <el-button type="success" plain @click="onProject(scope.row, 0)" v-hasPermi="['system:project:edit']">专利查看
          </el-button> -->
          <el-button type="primary" size="small" plain @click="to(scope.row,0)">导入专利</el-button>
          <el-button type="success" size="small" plain @click="to(scope.row,1)">专利查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="专家列表" align="center">
        <template slot-scope="scope">
          <el-button type="warning" plain @click="expert(scope.row,0)">导入专家</el-button>
          <!-- <el-button type="danger" plain @click="expert(scope.row,1)">查看专家</el-button> -->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['group:group:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['group:group:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 选择小组专家弹出框 -->
    <el-dialog title="选择小组专家" :visible.sync="open2" width="500px" append-to-body>
      <div style="display: flex; justify-content: center">
        <el-col :span="20" :xs="24">
          <el-table v-loading="loadding2" @selection-change="handleSelectionChange2" :data="userList" height="200px"
            row-key="id" ref="table2">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="用户编号" align="center" key="userId" prop="userId" />
            <el-table-column label="用户名称" align="center" key="userName" prop="userName" :show-overflow-tooltip="true" />
            <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName" :show-overflow-tooltip="true" />
          </el-table>
        </el-col>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="open2 = false">确 定</el-button>
        <el-button @click="open2 = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改小组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="小组名称" prop="groupName">
              <el-input v-model="form.groupName" placeholder="请输入小组名称" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 选择专利弹出框 -->
    <el-dialog :title="proTitle" :visible.sync="open3" width="766px" append-to-body>
      <div style="display: flex; justify-content: center">
        <el-col :span="20" :xs="24">
          <el-table v-loading="loadding3" @selection-change="handleSelectionChange3" :data="proList" height="200px"
            row-key="id" ref="table3" :empty-text="
              proTitle == '已导入专利' ? '你还未导入专利' : '没有可导入的专利'
            ">
            <el-table-column type="selection" align="center" />
            <el-table-column label="专利编号" align="center" prop="id" width="90" />
            <el-table-column label="专利名称" align="center" prop="proName" />
            <el-table-column label="专利文件" align="center" prop="proFile" width="200">
              <template slot-scope="scope">
                <a class="fileStyle">{{ scope.row.proFile }}</a>
              </template>
            </el-table-column>
            <el-table-column label="专利描述" align="center" prop="proDescr" />
            <el-table-column label="上传人" align="center" prop="proAuthor" />
          </el-table>
        </el-col>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPros()" v-show="proTitle == '已导入专利' ? false : true">确 定</el-button>
        <el-button @click="open3 = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGroup,
  getGroup,
  delGroup,
  addGroup,
  updateGroup,
} from "@/api/system/group/group";
import { selectProByid } from "@/api/system/project/project"
export default {
  name: "Group",
  data () {
    return {
      listUser: null,
      open: false,
      open2: false,
      open3: false,
      // 遮罩层
      loadding2: false,
      loadding3: false,
      loading: true,
      proTitle: "",
      proIds: [],
      // 选中数组
      ids: [],
      // 对应类型专家列表
      userList: [],
      //对应专利列表
      proList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 小组类型列表
      postOptions: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小组表格数据
      groupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      scopeProject: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupName: null,
        groupType: null,
        createBy: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验

    };
  },
  created () {
    this.getList();
  },
  methods: {

    to (index, num) {
      if (num == 1) {
        this.$router.push({ path: '/group/show/', query: index })
      } else {
        this.$router.push({ path: '/group/add/', query: index })
      }
    },
    /** 查询小组列表 */
    getList () {
      this.loading = true;
      listGroup(this.queryParams).then((response) => {
        this.groupList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.groupList.forEach(e => {
          this.postOptions.forEach(e2 => {
            if (e.groupType == e2.postId) {
              e.postName = e2.postName
            }
          })
        })
      });
    },
    // 查询小组类型对应专家

    expert (item, index) {
      localStorage.setItem("groupId", JSON.stringify(item.id))
      selectProByid(item.id).then(res => {
        localStorage.setItem("proIds", JSON.stringify(res.data))
      })
      this.$router.push({ path: '/group/addexpert/', query: index })
    },
    //查询小组类型列表

    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        groupName: null,
        groupType: null,
        createBy: null,
        createTime: null,
        userIds: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 多选框选中数据
    handleSelectionChange3 (selection) {
      this.proIds = selection.map((item) => item.id);
    },
    // 多选框选中数据
    handleSelectionChange2 (selection) {
      this.listUser = selection;
      if (this.listUser.length > 5) {
        this.$message({
          showClose: true,
          message: "你最多只能选择5个专家",
          type: "warning",
        });
        this.listUser = selection.slice(0, 5);
        let tempArr = selection.slice(5);
        if (tempArr.length !== 0) {
          tempArr.forEach((ele) => {
            this.$refs.table2.toggleRowSelection(ele, false);
          });
        }
      }

      this.form.userIds = this.listUser.map((item) => item.userId);
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加小组";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids;
      getGroup(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改小组";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateGroup(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGroup(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getPostList();
            });

          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除小组编号为"' + ids + '"的数据项？')
        .then(function () {
          return delGroup(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "group/group/export",
        {
          ...this.queryParams,
        },
        `group_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
